package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.qo.CustomerQueryObject;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;


    //修改客户状态updateStatus
    @RequestMapping("/updateStatus")
    @ResponseBody
    @RequiresPermissions(value={"customer:updateStatus","客户新增或修改"},logical= Logical.OR)
    public JsonResult updateStatus(Long cid,Integer status){
        try{
            customerService.updateStatus(cid,status);
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult(false,"修改客户状态失败");
        }
        return new JsonResult(true,"修改客户状态操作成功!!!"); // localhost/customer/list.do
    }

    // 客户新增或修改
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value={"customer:saveOrUpdate","客户新增或修改"},logical= Logical.OR)
    public JsonResult saveOrUpdate(Customer customer){
        try{
            if(customer.getId() != null){
                customerService.update(customer);
            }else {
                customerService.save(customer);
            }
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult(false,"客户新增或修改操作失败");
        }
        return new JsonResult(true,"客户新增或修改操作成功!!!"); // localhost/customer/list.do
    }

    // 查询所有客户数据
    @RequestMapping("/potentialList")
    @RequiresPermissions(value={"customer:potentialList","客户列表"},logical= Logical.OR)
    public String list(Model model, @ModelAttribute("qo")CustomerQueryObject qo){
        //将客户状态设置成潜在客户,因为这个跳转的页面只是潜在客户的
        qo.setStatus(0);
        //todo 查询市场专员和市场经理的信息,共享到页面,然后在页面使用下拉框显示(页面下拉框回显数据)
        List<Employee> sellers = employeeService.querySellersByRoleSn(Role.ROLE_SN_MARKET,Role.ROLE_SN_MARKET_MANGER);
        model.addAttribute("sellers", sellers);
        //TODO 如果当前用户是admin或者是市场经理,那就可以看到全部的客户信息,否则该用户就只能看到自己负责的用户信息
        Subject subject = SecurityUtils.getSubject();
        Employee currentEmployee = (Employee) subject.getPrincipal();
        if(!(currentEmployee.isAdmin() || subject.hasRole(Role.ROLE_SN_MARKET_MANGER))){
            qo.setSellerId(currentEmployee.getId());
        }
        //todo 数据库中查询出,从字典明细中查出工作,来源
        List<SystemDictionaryItem> jobs =  systemDictionaryItemService.queryBySystemDictionaryJob("job");
        model.addAttribute("jobs", jobs);
        List<SystemDictionaryItem> sources =  systemDictionaryItemService.queryBySystemDictionarySource("source");
        model.addAttribute("sources", sources);

        //将交流方法,和跟进结果查出来,然后放到里面
        List<SystemDictionaryItem> types =  systemDictionaryItemService.queryBySystemDictionaryCommunicationMethod("communicationMethod");
        model.addAttribute("types", types);
        List<SystemDictionaryItem> traceResult =  systemDictionaryItemService.queryBySystemDictionarytraceResult("traceResult");
        model.addAttribute("traceResult", traceResult);

        PageInfo query = customerService.query(qo);
        model.addAttribute("pageResult", query);
        return "customer/potentialList";
    }
}
